package com.android.launcher3;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.os.SystemClock;

public class PageIndicator {
	Drawable mMoreDrawable;
	Drawable mPageDrawable;

	Paint mTextPaint;
	Page[] mPage;

	boolean mEnableShowHide;
	boolean mEnableTouch;
	boolean mIsDraw;
	boolean mIsHiding;
	boolean mIsVisibleLeftMore;
	boolean mIsVisibleRightMore;

	int mCurrentPage = -1;
	int mFirstTextIndex;
	int mGap;
	int mLeft;
	int mMoreGap;
	int mTextSize;
	int mTop;
	int mTotalPageCount;
	int mTwoNumTextSize;

	private static final int ANIMATION_DURATION = 200;
	private static final int ANIMATION_STATE_STARTING = 1;
	private static final int ANIMATION_STATE_RUNNING = 2;
	private static final int ANIMATION_STATE_DONE = 3;
	private static final int DRAW_STATE_NONE = 0;
	private static final int DRAW_STATE_SMALL = 1;
	private static final int DRAW_STATE_MIDDLE = 2;
	private static final int DRAW_STATE_LARGE = 3;
	public static final int INVALID_AREA = 255;
	public static final int LEFT_MORE_AREA = 254;
	public static final int RIGHT_MORE_AREA = 253;
	public static final int VISIBLE_PAGEINDICATOR_COUNT_MAX = 9;
	private static final float[] rate = { 0, 0.45f, 0.85f, 1.4f };
	public static final String TAG = "PageIndicator";

	/**
	 * Creates a new PageIndicator object.
	 */
	public PageIndicator() {
		this.mTextPaint = new Paint();
		this.mTextPaint.setAntiAlias(true);
		this.mTextPaint.setTextAlign(Paint.Align.CENTER);
		this.mTextPaint.setColor(-16777216);
		this.mIsDraw = true;
		this.mEnableTouch = true;
	}

	public void enableShowHide(final boolean paramBoolean) {
		this.mEnableShowHide = paramBoolean;

		if (!paramBoolean) {
			this.mIsDraw = true;

			if (paramBoolean) {
				this.mEnableTouch = false;
			}
		}
	}

	public void hide() {
		if (!this.mEnableShowHide) {
			return;
		}

		int i = this.mTotalPageCount;
		int j = 0;

		while (j < i) {
			this.mPage[j].setDrawState(DRAW_STATE_NONE);
			j++;
		}

		this.mIsHiding = true;
	}

	public void setMoreDrawable(final Drawable paramDrawable) {
		this.mMoreDrawable = paramDrawable;
	}

	public void setPageDrawable(final Drawable d) {
		this.mPageDrawable = d;
	}

	public void show() {
		if ((!this.mEnableShowHide) || (this.mIsDraw)) {
			return;
		}

		boolean bool = this.mIsDraw;
		this.mIsDraw = true;
		this.mIsHiding = false;

		int i = this.mTotalPageCount;
		int j = 0;

		while ((j < i) && (!bool)) {
			this.mPage[j].initDrawState();
			j++;
		}
	}

	/**
	 * return false 则动画还没有完成,需要继续调用draw。
	 * 
	 * @param canvas
	 * 
	 * @return
	 */
	boolean draw(final Canvas canvas) {
		// LogUtils.d(TAG, "draw-start");

		// Log.e(TAG,"draw PageIndicator  start ");
		if (this.mPageDrawable == null) {
			return false;
		}

		int totalPageCount = this.mTotalPageCount;
		int gap = this.mGap;
		int moreGap = this.mMoreGap;

		int save = canvas.save();

		canvas.translate(this.mLeft, this.mTop);

		// 左边开始
		if ((this.mIsVisibleLeftMore) && (this.mMoreDrawable != null)) {
			int i2 = this.mMoreDrawable.getIntrinsicWidth() + moreGap;
			canvas.translate(-i2, 0.0F);
			this.mMoreDrawable.draw(canvas);
			canvas.translate(i2, 0.0F);
		}

		// 左边结束

		// 中间开始 中间的动画完成了则全部完成，否则继续draw.
		int j = 1;

		for (int i = 0; i < totalPageCount; i++) {
			Page localPage = this.mPage[i];
			int i4 = this.mFirstTextIndex + i;

			if (!localPage.draw(canvas, i4)) {
				j = 0;
			}

			float f5 = this.mPageDrawable.getIntrinsicWidth() + gap;
			canvas.translate(f5, 0.0F);
		}

		// 中间结束

		// 右边开始
		if ((this.mIsVisibleRightMore) && (this.mMoreDrawable != null)) {
			canvas.translate(moreGap - gap, 0.0F);
			this.mMoreDrawable.draw(canvas);
		}

		canvas.restoreToCount(save);

		// 右边结束
		if (j == 0) {
			// LogUtils.d(TAG, "draw-end=false");

			// Log.e(TAG,"3333333  draw PageIndicator  end ");
			return false;
		}

		// LogUtils.d(TAG, "draw-end=true");

		// Log.e(TAG,"44444444444 draw PageIndicator  end ");
		return true;

		// if ( && (this.mIsHiding))
		// {
		// this.mIsHiding = false;
		// this.mIsDraw = false;
		// Log.e(TAG,"draw PageIndicator  end ");
		// return false ;
		//
		// }
		// else
		// {
		// Log.e(TAG,"draw PageIndicator  end ");
		// return true;
		// }
	}

	void enableLeftMore(final boolean paramBoolean) {
		this.mIsVisibleLeftMore = paramBoolean;
	}

	void enableRightMore(final boolean paramBoolean) {
		this.mIsVisibleRightMore = paramBoolean;
	}

	/**
	 * 不完整
	 * 
	 * @param paramInt1
	 * @param paramInt2
	 * 
	 * @return
	 */
	int getPageTouchArea(final int paramInt1, final int paramInt2) {
		return -1;
	}

	void setCurrentPage(final int currentPage, final boolean paramBoolean) {
		// LogUtils.d(TAG,
		// "setCurrentPage-currentPage=" + currentPage + ", prePage="
		// + this.mCurrentPage);

		this.mCurrentPage = currentPage;

		if (this.mIsHiding) {
			return;
		}
		this.mPage[mCurrentPage].setDrawState(DRAW_STATE_MIDDLE);

		for (int j = 0; j < this.mCurrentPage; j++) {
			this.mPage[j].setDrawState(DRAW_STATE_SMALL);
		}

		for (int j = this.mCurrentPage + 1; j < this.mTotalPageCount; j++) {
			this.mPage[j].setDrawState(DRAW_STATE_SMALL);
		}
	}

	void setFirstTextNum(final int paramInt) {
		this.mFirstTextIndex = paramInt;
	}

	void setGap(final int paramInt) {
		setGap(paramInt, paramInt);
	}

	void setGap(final int paramInt1, final int paramInt2) {
		this.mGap = paramInt1;
		this.mMoreGap = paramInt2;
	}

	void setOffset(final int paramInt1, final int paramInt2) {
		this.mLeft = paramInt1;
		this.mTop = paramInt2;

		// Log.e(TAG,"mTop =  "+ mTop);
		// Log.e(TAG,"mLeft =  "+ mLeft);
	}

	void setPageCount(int pageCount) {
		if (pageCount > VISIBLE_PAGEINDICATOR_COUNT_MAX) {
			pageCount = VISIBLE_PAGEINDICATOR_COUNT_MAX;
		}

		if (this.mTotalPageCount != pageCount) {
			this.mPage = new Page[pageCount];

			int i = 0;

			while (i < pageCount) {
				this.mPage[i] = new Page();
				this.mPage[i].setId(i + 1);
				i++;
			}
		}

		this.mTotalPageCount = pageCount;
	}

	void setTextSize(final int paramInt) {
		this.mTextSize = paramInt;

		Paint localPaint = this.mTextPaint;
		float f = this.mTextSize;
		localPaint.setTextSize(f);
	}

	void setTwoNumTextSize(final int paramInt) {
		this.mTwoNumTextSize = paramInt;
	}

	public class Page {
		float mAnimationFrom;
		float mAnimationTo;
		int id = -1;
		int mAnimationDuration = ANIMATION_DURATION;
		int mAnimationState = ANIMATION_STATE_DONE;
		int mDrawState = DRAW_STATE_SMALL;
		int mPrevDrawState = DRAW_STATE_SMALL;
		long mAnimationStartTime;
		private static final String TAG = "Page ";

		/**
		 * Creates a new Page object.
		 */
		public Page() {
			// Log.e(TAG, TAG + "new   Page  77777");
		}

		public int getId() {
			return id;
		}

		public void setId(final int id) {
			this.id = id;

			// Log.e(TAG, TAG + "setId  "+id+ " 77777");
		}

		boolean draw(final Canvas canvas) {
			return draw(canvas, -1);
		}

		boolean draw(final Canvas canvas, final int index) {
			// LogUtils.d(TAG,
			// "Page-draw-index=" + index + ", mAnimationState="
			// + mAnimationState);

			// Log.e(TAG, TAG + " draw");
			// Log.e(TAG,"draw Page  start");
			if (this.mAnimationState == ANIMATION_STATE_STARTING) {
				// Log.e(TAG, TAG + " ANIMATION_STATE_STARTING " +id);
				this.mAnimationStartTime = SystemClock.uptimeMillis();
				this.mAnimationState = ANIMATION_STATE_RUNNING;
			}

			Paint localPaint = PageIndicator.this.mTextPaint;

			if (this.mAnimationState == ANIMATION_STATE_RUNNING) {
				long l2 = SystemClock.uptimeMillis();
				long l3 = this.mAnimationStartTime;
				float f1 = (float) (l2 - l3);
				float f3 = f1 / mAnimationDuration;

				if (f3 >= 1.0F) {
					this.mAnimationState = ANIMATION_STATE_DONE;
					f3 = 1.0F;
				}

				float f9 = this.mAnimationFrom
						+ (this.mAnimationTo - this.mAnimationFrom) * f3;
				canvas.save();

				float f12 = mPageDrawable.getIntrinsicWidth() * (1.0F - f9)
						/ 2.0F;
				canvas.translate(f12, f12);
				canvas.scale(f9, f9);
				PageIndicator.this.mPageDrawable.draw(canvas);

				if (index != -1) {
					if (f9 > PageIndicator.rate[DRAW_STATE_SMALL]
							&& f9 < PageIndicator.rate[DRAW_STATE_MIDDLE]) {
						float f16 = mTextSize;
						localPaint.setTextSize(f16);

						float f21 = (localPaint.ascent() + localPaint.descent()) / 2.0F;
						float f22 = mPageDrawable.getIntrinsicWidth() / 2 - f21;
						canvas.drawText((index + 1) + "",
								mPageDrawable.getIntrinsicWidth() / 2, f22,
								localPaint);
					}
				}

				canvas.restore();

				if (index != -1 && f9 > PageIndicator.rate[DRAW_STATE_MIDDLE]) {
					localPaint.setTextSize(mTextSize * rate[DRAW_STATE_MIDDLE]);

					float f29 = (localPaint.ascent() + localPaint.descent()) / 2.0F;
					float f30 = mPageDrawable.getIntrinsicWidth() / 2 - f29;
					canvas.drawText((index + 1) + "",
							mPageDrawable.getIntrinsicWidth() / 2, f30,
							localPaint);
				}

				if (this.mAnimationState != ANIMATION_STATE_DONE) {
					// LogUtils.d(TAG, "Page-draw-end=false");

					return false;
				}
			}

			if (this.mAnimationState == ANIMATION_STATE_DONE) {
				// Log.e(TAG, TAG + "  starting  ANIMATION_STATE_DONE " +id);
				canvas.save();

				float f33 = mPageDrawable.getIntrinsicWidth()
						* (1.0F - rate[this.mDrawState]) / 2.0F;
				canvas.translate(f33, f33);
				canvas.scale(rate[this.mDrawState], rate[this.mDrawState]);
				PageIndicator.this.mPageDrawable.draw(canvas);
				canvas.restore();

				if (index == -1) {
					// Log.e(TAG, TAG + " ANIMATION_STATE_DONE   no  text" +id);
					// LogUtils.d(TAG, "Page-draw-end=true0");

					return true;
				}

				// middle以下的都不写数字
				if (rate[this.mDrawState] < rate[DRAW_STATE_MIDDLE]) {
					// LogUtils.d(TAG, "Page-draw-end=true1");
					return true;
				}

				localPaint.setTextSize(mTextSize * rate[DRAW_STATE_MIDDLE]);

				// Log.e(TAG, TAG + " " +mTextSize
				// * rate[DRAW_STATE_MIDDLE]);
				float f43 = (localPaint.ascent() + localPaint.descent()) / 2.0F;

				canvas.drawText((index + 1) + "",
						mPageDrawable.getIntrinsicWidth() / 2,
						mPageDrawable.getIntrinsicWidth() / 2 - f43, localPaint);

			}

			// LogUtils.d(TAG, "Page-draw-end=true2");

			// Log.e(TAG,"draw Page  end");
			return true;
		}

		void initDrawState() {
			this.mPrevDrawState = DRAW_STATE_NONE;
			this.mDrawState = DRAW_STATE_NONE;
		}

		void setDrawState(final int newDrawState) {
			// LogUtils.d(TAG,
			// "Page-setDrawState-new=" + newDrawState + ", old="
			// + mDrawState);
			this.mPrevDrawState = mDrawState;

			// if (newDrawState == DRAW_STATE_SMALL) {
			// Log.e(TAG," newDrawState==1 is  Here      7777777" );
			// }

			this.mDrawState = newDrawState;

			if (mDrawState != mPrevDrawState) {
				this.mAnimationState = ANIMATION_STATE_STARTING;
				this.mAnimationTo = PageIndicator.rate[mDrawState];
				this.mAnimationFrom = PageIndicator.rate[mPrevDrawState];

				// Log.e(TAG," DrawState Changed   444444444" );
			} else {
				// Log.e(TAG,"Same DrawState         44444444444");
			}
		}
	}
}
